{include file="public/head"/} 
  <body>
    
    <div class="x-body">
      <div class="layui-row">
        选择要授权的设备
      </div>
      <xblock>
       
       
      </xblock>
      <table class="layui-table" id="table">
        <thead>
          <tr>
            <th>
              <div class="layui-unselect header layui-form-checkbox " lay-skin="primary"><i class="layui-icon">&#xe605;</i></div>
            </th>
            <th>门锁名称</th>
            <th>序列号</th>

          </tr>
        </thead>
        <tbody>
          {foreach $devicelist as $lis}
          <tr>
            <td>
              <div class="layui-unselect layui-form-checkbox <?php if(in_array($lis['id'], $idarr)){?>layui-form-checked<?php }?>" lay-skin="primary" data-id='{$lis.id}' checked><i class="layui-icon">&#xe605;</i></div>
            </td>
            <td>{$lis.devicename}</td>
            <td>{$lis.devicekey}</td>
          </tr>
          {/foreach}
        </tbody>
      </table>
      <div class="layui-form-item">
          <label for="L_repass" class="layui-form-label">
          </label>
          <button  class="layui-btn" lay-filter="add" lay-submit="" onclick="accredit()">
              授权
          </button>


      </div>
      <div class="page">
        <div>
         
        </div>
      </div>

    </div>
    <script type="text/javascript">
      function accredit(){
        var datas = $('#table tbody td .layui-form-checked')
        var ids = []
        datas.each(function(index, element){
          var id = $(this).data('id')
          ids.push(id)
        })

        var deviceids = ids.join(',');
        var count = ids.length
        if(count=="0"){
          alert("请选择要授权的设备");
          return false;
        }
        var wxuser_id='{$wxuser_id}';
        var url='{:url("deviceaccredit")}?wxuser_id='+wxuser_id+"&deviceids="+deviceids;
        $.post('{:url("deviceauth")}', {wxuser_id: wxuser_id,deviceids:deviceids}, function(r){
          if(r.code=="0"){
            alert('设备授权成功');
            var index = parent.layer.getFrameIndex(window.name);  
            parent.layer.close(index);//关闭当前页  
            //window.parent.location.replace(location.href)//刷新父级页面

            //window.parent.location.reload();
          }
          
        })
      }

      function cancelaccredit(){
          var datas = $('#table tbody td .layui-form-checked')
          var ids = []
          datas.each(function(index, element){
              var id = $(this).data('id')
              ids.push(id)
          })

          var deviceids = ids.join(',');
          var count = ids.length
          if(count=="0"){
              alert("请选择要销权的设备");
              return false;
          }
          var wxuser_id='{$wxuser_id}';

          $.post('{:url("devicecancelaccredit")}', {wxuser_id: wxuser_id,deviceids:deviceids}, function(r){
              if(r.code=="0"){
                  alert('设备销权成功');
                  var index = parent.layer.getFrameIndex(window.name);
                  parent.layer.close(index);//关闭当前页
                  //window.parent.location.replace(location.href)//刷新父级页面

                  //window.parent.location.reload();
              }

          })
      }
    </script>
    <script>
      var _hmt = _hmt || []; (function() {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?b393d153aeb26b46e9431fabaf0f6190";
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(hm, s);
      })();
    </script>
  </body>

</html>